//
//  ViewController.m
//  UI26_Animation
//
//  Created by Scott on 16/1/18.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *viewOfGray;
@property (weak, nonatomic) IBOutlet UIView *viewOfRed;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



#pragma mark - 知识点1: UIView实现动画(1/2)
- (IBAction)handleView1:(id)sender {

    // API 1:
//    [UIView animateWithDuration:2 animations:^{
//        // 设置控件的属性值.
//        self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
//        
//        self.viewOfGray.backgroundColor = [UIColor redColor];
//    }];
    
    
    // API 2:
//    [UIView animateWithDuration:2 animations:^{
//        self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
//        self.viewOfGray.backgroundColor = [UIColor redColor];
//        
//        
//    } completion:^(BOOL finished) {
//        
//        self.viewOfGray.backgroundColor = [UIColor lightGrayColor];
//        
//    }];
    
    // API 3: options参数: 控制动画效果.
//    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
//        
//        self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
//        self.viewOfGray.backgroundColor = [UIColor redColor];
//        
//    } completion:^(BOOL finished) {
//        
//    }];
    
    // API 4:
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:10 options:0 animations:^{
        
        
        [UIView setAnimationRepeatCount:2];
        
        self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
    } completion:^(BOOL finished) {
    }];
}

#pragma mark - 知识点2: UIView动画2
- (IBAction)handleView2:(id)sender {
    
    // 准备开始动画
    [UIView beginAnimations:@"abc" context:nil];
    
    // 设置动画参数
    [UIView setAnimationDuration:2];
    [UIView setAnimationRepeatCount:CGFLOAT_MAX];
    [UIView setAnimationRepeatAutoreverses:YES];
    
    // 控件属性的更改
    self.viewOfGray.frame = CGRectMake(0, 200, 375, 120);
    self.viewOfGray.backgroundColor = [UIColor redColor];
    
    // 提交动画.
    [UIView commitAnimations];
}


#pragma mark - 知识点3: CABasicAnimation
- (IBAction)handleBasic:(id)sender {
    
    
    // 创建动画对象.
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    
    // 动画对象的API设置.
    basic.fromValue = @1;
    basic.toValue = @0.5;
    
    basic.duration = 1;
    basic.autoreverses = YES;
    basic.repeatCount = CGFLOAT_MAX;
    
    
    // layer层上添加动画.
    [self.viewOfGray.layer addAnimation:basic forKey:@"basic"];

}


#pragma mark - 知识点4: CAAnimationGroup
- (IBAction)handleAnimationGroup:(id)sender {
    
    // 创建动画组对象.
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    
    CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    basic1.fromValue = @1;
    basic1.toValue = @0.2;
    
    CABasicAnimation *basic2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    basic2.fromValue = @0;
    basic2.toValue = @M_PI;
    
    
    group.animations = @[basic1, basic2];
    group.duration = 2;
    group.autoreverses = YES;
    group.repeatCount = CGFLOAT_MAX;
    
    
    // layer层添加动画对象.
    [self.viewOfGray.layer addAnimation:group forKey:@"group"];
    
}

#pragma mark - 知识点5: CAKeyFrameAnimation
- (IBAction)handleKeyFrame:(id)sender {

    // 创建动画对象
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    // 设置动画的API
    
    // 通过函数创建CGPathRef结构体.
    CGMutablePathRef path = CGPathCreateMutable();
    
    // 起始点
    CGPathMoveToPoint(path, nil, self.viewOfRed.center.x, self.viewOfRed.center.y);
    
    // 路径经过的点(直线移动).
//    CGPathAddLineToPoint(path, nil, 200, 200);
//    CGPathAddLineToPoint(path, nil, 200, 300);
//    CGPathAddLineToPoint(path, nil, 300, 300);
    
    
    // 路径经过的点(曲线移动).
    CGPathAddCurveToPoint(path, nil, self.viewOfRed.center.x, self.viewOfRed.center.y, self.viewOfRed.center.x, self.viewOfRed.center.y + 100, self.viewOfRed.center.x + 100, self.viewOfRed.center.y + 100);
    
    
    keyFrame.path = path;
    keyFrame.duration = 2;
    keyFrame.autoreverses = YES;
    keyFrame.repeatCount = CGFLOAT_MAX;
    

    // layer层添加动画.
    [self.viewOfRed.layer addAnimation:keyFrame forKey:@"keyFrame"];
}


#pragma mark - 知识点6: CATransition
- (IBAction)handleTransition:(id)sender {
    
    // 创建动画对象.
    CATransition *sition = [CATransition animation];
    
    // 设置动画API
    sition.type = @"cameraIrisHollowOpen";
    
    sition.duration = 2;
    sition.autoreverses = YES;
    sition.repeatCount = CGFLOAT_MAX;
    
    
    // layer上添加动画.
    [self.viewOfGray.layer addAnimation:sition forKey:@"sition"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
